package com.gateway.system.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gateway.common.core.page.PageResult;
import com.gateway.common.enums.ResultEnum;
import com.gateway.common.exception.BusinessException;
import com.gateway.common.utils.AjaxResult;
import com.gateway.common.utils.StringUtils;
import com.gateway.config.superclass.SuperService;
import com.gateway.system.domain.dto.BasicRouteDto;
import com.gateway.system.domain.dto.FilterUrlDto;
import com.gateway.system.domain.dto.WhiteIpDto;
import com.gateway.system.domain.entity.BasicRoute;
import com.gateway.system.domain.entity.FilterUrl;
import com.gateway.system.domain.entity.WhiteIp;
import com.gateway.system.domain.vo.FilterUrlVo;
import com.gateway.system.domain.vo.WhiteIpVo;
import com.gateway.system.mapper.WhiteIpMapper;
import com.gateway.system.service.WhiteIpService;
import lombok.extern.slf4j.Slf4j;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Huangkai
 */

@Slf4j
@Service
public class WhiteIpServiceImpl extends SuperService<WhiteIpMapper, WhiteIp> implements WhiteIpService {

    @Autowired
    WhiteIpMapper whiteIpMapper;

    @Autowired
    WhiteIpService whiteIpService;

    @Override
    public AjaxResult selectFilterUrlList() {
        LambdaQueryWrapper<WhiteIp> wrapper = new QueryWrapper<WhiteIp>().lambda();
        List<WhiteIp> whiteIpList = whiteIpMapper.selectList(wrapper);
        if(whiteIpList == null || whiteIpList.size() <= 0){
            throw new BusinessException(ResultEnum.DATA_NOT_EXIST);
        }
        String arrayStr = JSONArray.toJSONString(whiteIpList);
        JSONArray jsonArray = JSONArray.parseArray(arrayStr);
        List<WhiteIpDto> whiteIpDtoList = jsonArray.toJavaList(WhiteIpDto.class);
        return AjaxResult.ResultWithDataSuccess(whiteIpDtoList);
    }

    @Override
    public WhiteIpDto selectWhiteIp(String ip) {
        LambdaQueryWrapper<WhiteIp> wrapper = new QueryWrapper<WhiteIp>().lambda();
        wrapper.eq(WhiteIp::getIp, ip);
        WhiteIp whiteIp = whiteIpMapper.selectOne(wrapper);
        WhiteIpDto whiteIpDto = null;
        if(whiteIp != null){
            Mapper mapper = new DozerBeanMapper();
            whiteIpDto = mapper.map(whiteIp, WhiteIpDto.class);
        }
        return whiteIpDto;
    }

    @Override
    public AjaxResult insertWhiteIp(WhiteIpVo whiteIpVo) {
        WhiteIpDto whiteIpDto = whiteIpService.selectWhiteIp(whiteIpVo.getIp());
        if(whiteIpDto != null){
            throw new BusinessException(ResultEnum.DATA_EXIST);
        }
        WhiteIp whiteIp = new WhiteIp();
        whiteIp.setUuid(StringUtils.getSnowFlakeId());
        whiteIp.setIp(whiteIpVo.getIp());
        whiteIp.setIpRange(whiteIpVo.getIpRange());
        whiteIp.setIpWay(whiteIpVo.getIpWay());
        whiteIp.setType(whiteIpVo.getType());
        whiteIp.insert();
        return AjaxResult.success();
    }

    @Override
    public AjaxResult deleteAjaxResult(WhiteIpVo whiteIpVo) {
        LambdaQueryWrapper<WhiteIp> wrapper = new QueryWrapper<WhiteIp>().lambda();
        wrapper.eq(WhiteIp::getUuid, whiteIpVo.getUuid());
        whiteIpMapper.delete(wrapper);
        return AjaxResult.success();
    }
}
